package com.wing.order.controller.admin;

import com.wing.common.utils.PageResult;
import com.wing.enterprise.feign.EnterpriseClient;
import com.wing.enterprise.product.model.response.ProductDetailVO;
import com.wing.product.service.ProductDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author
 */
@Validated
@Api(value = "Admin-在库产品列表", tags = "#/order/inventoryProduct")
@RestController("inventoryProductController")
@RequestMapping("order/admin/inventoryProduct")
@RequiredArgsConstructor
public class InventoryProductController {

    private final EnterpriseClient enterpriseClient;
    private final ProductDetailService productDetailService;

    @ApiOperation(value = "查询所有在库产品", notes = "搜索")
    @GetMapping("/list")
    public PageResult<ProductDetailVO> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(defaultValue = "create_time") String sort,
            @RequestParam(defaultValue = "asc") String order,
            @RequestParam(value = "skuName", required = false) String skuName,
            @RequestParam(value = "skuCode", required = false) String skuCode,
            @RequestParam(value = "productTypeId", required = false) Long productTypeId,
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "type", required = false) Integer type) {
        //搜索
        PageResult<ProductDetailVO> list = enterpriseClient.getInventoryProductDetailPageList(page, limit, skuName, skuCode, productTypeId, id, null);
        //按照降序排序
        List<ProductDetailVO> data = list.getData();
        data = list.getData().stream().
                sorted(Comparator.comparing(ProductDetailVO::getCreateTime).
                        reversed()).collect(Collectors.toList());
//        for (ProductDetailVO item : list.getData()) {
//            //库存来源是刀具签收入库，需要查询cab库的产品相关信息
//            if (item.getType().name().equals("knife")) {
//                com.wing.product.model.response.ProductDetailVO detailVo = productDetailService.getProductInfoBySkuCode(item.getSkuCode());
//                if (detailVo != null) {
//                    if (item.getSkuCode().equals(detailVo.getSkuCode())) {
//                        item.setProductName(detailVo.getProductName());
//                        item.setBarcode(detailVo.getBarcode());
//                        item.setSkuSpecValueNames(detailVo.getSkuSpecValueNames());
//                        item.setCategoryName(detailVo.getProductCategoryName());
//                        item.setUnitName(detailVo.getUnitName());
//                        item.setSupplyPrice(detailVo.getSupplyPrice());
//                        item.setProductPic(detailVo.getProductPic());
//                    }
//                }
//            }
//        }
        return new PageResult<>(data, list.getCount());
    }
}
